package com.siyu.gis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siyu.basedata.rsvrgis.domain.RsvrGis;
import com.siyu.basedata.rsvrgis.service.IRsvrGisService;
import com.siyu.common.constant.MinioOrOssConstants;
import com.siyu.common.core.domain.AjaxResult;
import com.siyu.common.utils.aliyun.service.FileOssStorageService;
import com.siyu.common.utils.minio.service.FileStorageService;
import com.siyu.common.utils.uuid.IdUtils;
import com.siyu.gis.service.GisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Tainted;
import java.io.IOException;

@Service
public class GisServiceImpl implements GisService {

    private static Boolean minioOrOss = MinioOrOssConstants.getMinioOrOss();

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private IRsvrGisService rsvrGisService;

    @Autowired
    private FileOssStorageService fileOssStorageService;

    @Override
    @Transactional
    public ResponseEntity<AjaxResult> uploadgeo(MultipartFile rsvrUrl, MultipartFile upBasinUrl, MultipartFile downBasinUrl, MultipartFile upRiverUrl, MultipartFile downRiverUrl, String rscd) {
        try{
            QueryWrapper<RsvrGis> rsvrGisQueryWrapper = new QueryWrapper<>();
            rsvrGisQueryWrapper.eq("rscd", rscd);
            // 判断水库编码是否在数据库中存在   存在更新,不存在添加
            RsvrGis rsvrGis = rsvrGisService.getOne(rsvrGisQueryWrapper);
          if (rsvrGis == null){
              String id = IdUtils.getId();
              rsvrGis = new RsvrGis();
              rsvrGis.setRscd(rscd);
              rsvrGis.setId(id);
              if(rsvrUrl!=null){
                  String filename = id+"_rsvrUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/rsvrUrl", filename, rsvrUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/rsvrUrl", filename, rsvrUrl.getInputStream());
                  }
                  rsvrGis.setRsvrUrl(pathUrl);
              }
              if(upBasinUrl!=null){
                  String filename = id+"_upBasinUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/upBasinUrl", filename, upBasinUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/upBasinUrl", filename, upBasinUrl.getInputStream());
                  }
                  rsvrGis.setUpBasinUrl(pathUrl);
              }
              if(downBasinUrl!=null){
                  String filename = id+"_downBasinUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/downBasinUrl", filename, downBasinUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/downBasinUrl", filename, downBasinUrl.getInputStream());
                  }
                  rsvrGis.setDownBasinUrl(pathUrl);
              }
              if(upRiverUrl!=null){
                  String filename = id+"_upRiverUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/upRiverUrl", filename, upRiverUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/upRiverUrl", filename, upRiverUrl.getInputStream());
                  }
                  rsvrGis.setUpRiverUrl(pathUrl);
              }
              if(downRiverUrl!=null){
                  String filename = id+"_downRiverUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/downRiverUrl", filename, downRiverUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/downRiverUrl", filename, downRiverUrl.getInputStream());
                  }
                  rsvrGis.setDownRiverUrl(pathUrl);
              }
              rsvrGisService.save(rsvrGis);
          }else {
              if(rsvrUrl!=null){
                  String filename = rsvrGis.getId()+"_rsvrUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/rsvrUrl", filename, rsvrUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/rsvrUrl", filename, rsvrUrl.getInputStream());
                  }
                  rsvrGis.setRsvrUrl(pathUrl);
              }
              if(upBasinUrl!=null){
                  String filename = rsvrGis.getId()+"_upBasinUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/upBasinUrl", filename, upBasinUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/upBasinUrl", filename, upBasinUrl.getInputStream());
                  }
                  rsvrGis.setUpBasinUrl(pathUrl);
              }
              if(downBasinUrl!=null){
                  String filename = rsvrGis.getId()+"_downBasinUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/downBasinUrl", filename, downBasinUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/downBasinUrl", filename, downBasinUrl.getInputStream());
                  }
                  rsvrGis.setDownBasinUrl(pathUrl);
              }
              if(upRiverUrl!=null){
                  String filename = rsvrGis.getId()+"_upRiverUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/upRiverUrl", filename, upRiverUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/upRiverUrl", filename, upRiverUrl.getInputStream());
                  }
                  rsvrGis.setUpRiverUrl(pathUrl);
              }
              if(downRiverUrl!=null){
                  String filename = rsvrGis.getId()+"_downRiverUrl.geojson";
                  String pathUrl = "";
                  if (minioOrOss){
                      pathUrl = fileOssStorageService.uploadGeoJsonFile("rsvrgis/downRiverUrl", filename, downRiverUrl.getInputStream());
                  }else {
                      pathUrl = fileStorageService.uploadGeoJsonFile("rsvrgis/downRiverUrl", filename, downRiverUrl.getInputStream());
                  }
                  rsvrGis.setDownRiverUrl(pathUrl);
              }
              rsvrGisService.updateById(rsvrGis);
          }
            return ResponseEntity.ok(AjaxResult.success("文件上传成功"));
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(AjaxResult.error("文件上传时发生错误: " + e.getMessage()));
        }
    }
}
